package com.graducationdesign.springboot.controller.Student;

import com.graducationdesign.springboot.entities.Administrator.GraducationDesignListMoudle.AttachmentInformation;
import com.graducationdesign.springboot.entities.Administrator.PersonnalInformationMoudle.Student;
import com.graducationdesign.springboot.mapper.AttachmentInformationMapper;
import com.graducationdesign.springboot.mapper.StudentMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.zhneg
 * @create 2019-12-02-21:48
 */
@Controller
public class MyGraducationDesignForStuController {

    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());

    @Autowired
    AttachmentInformationMapper attachmentInformationMapper;

    @Autowired
    StudentMapper studentMapper;
    @GetMapping("/mydesigns")
    public String list(Map<String,Object> map, AttachmentInformation attachmentInformation){

        logger.info("我的附件"+attachmentInformation);
        List<AttachmentInformation> attachmentInformations=attachmentInformationMapper.getAttachmentInformation(attachmentInformation);
        logger.info("查询操作：附件信息查询结果。。。"+attachmentInformations);

        map.put("attachmentInformation",attachmentInformations);


        return "Student/graduationDesignMoudle/mygraductiondesignlist";
    }


    // 我的毕设=======上传附件
    @RequestMapping("/uploadFile")
    public String uploadFile(HttpServletRequest request,@RequestPart("file") MultipartFile file) {

        if (file.isEmpty()){
            return "Student/graduationDesignMoudle/mygraductiondesignlist";
        }
        // 获得文件名称
        String fileName = file.getOriginalFilename();
        // 定义存储路径
        String filePath = "gulimall-hello-zlj.oss-cn-beijing.aliyuncs"+fileName;

//        File dest = new File(filePath+fileName);

        try {
            // 保存文件
//            file.transferTo(dest);
            logger.info("上传成功");

            AttachmentInformation fuJian = new AttachmentInformation();
            Date date = new Date();
            // 保存数据
            fuJian.setName(fileName);
            fuJian.setCreatetime(date);

            fuJian.setFilepath(filePath);
//            return "上传成功";.java.lang.ClassCastException: java.lang.String cannot be cast to com.graducationdesign.springboot.entities.Administrator.PersonnalInformationMoudle.Student

            logger.info((String) request.getSession().getAttribute("loginUser"));

            String stu = (String) request.getSession().getAttribute("loginUser");
            Student student=new Student();
            student.setLoginUserName(stu);
            logger.info(String.valueOf(student));
            Student students =studentMapper.getStudentForId(student);
            fuJian.setSid(students.getId());

            attachmentInformationMapper.save(fuJian);
            logger.info("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
//        return "Student/graduationDesignMoudle/mygraductiondesignlist";
        return "redirect:/mydesigns";
    }

}
